package handlers

import (
	"encoding/json"
	"order-ease/cache"
	"order-ease/models"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var data []models.EaseOrder
	redis_key := "list_" + strconv.Itoa(page)
	result, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if result == "" {
		models.DB.Offset(offset).Limit(size).Find(&data)
		marshal, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, marshal, time.Hour*2)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": data,
	})
}
